<template>
  <div>
    <!-- 内容 start -->
    <div class="contenenr">
      <!-- 标题头部 start -->
      <div class="article_title">
        <div>
          <div class="title_text">
            <h1>{{data.title}}</h1>
          </div>
          <div class="article_data">
            <div class="article_creatTime">{{new Date(data.createTime).format('yyyy-MM-dd')}}</div>
            <div class="article_data_num">
              <div>
                <span class="article_browseNum"></span>
                <img :src="eyegUrl" />
                {{data.browseNum}}
              </div>
              <div>
                <span class="article_commentNum"></span>
                <img :src="reviewbgUrl" />
                {{data.commentNum}}
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 标题头部 end -->
      <!-- 文章内容 start -->
      <div class="article_nav">
        <div v-html="data.content"></div>
      </div>
      <!-- 文章内容 end -->
      <!-- 评论 start -->
      <div class="review">
        <div>
          <div>评论</div>
          <div>
            <textarea rows="8" cols="20" id="textareaValue" v-model="value"></textarea>
          </div>
          <div>
            <button class="button" @click="review_btn">发表</button>
          </div>
        </div>
      </div>
      <!-- 评论 end -->
      <!-- 评论 list  start -->
      <div class="allList_text">
        <div>全部评论</div>
      </div>
      <div class="review_list">
        <div v-for="(item,index) in comments" :key="index">
          <div class="review_content">{{item.content}}</div>
          <div class="review_time">{{new Date(item.createTime).format('yyyy-MM-dd')}}</div>
        </div>
      </div>
      <!-- 评论 list  end -->
    </div>
    <!-- 内容 end -->
  </div>
</template>
<script>
import axios from "~/plugins/axios";
import { dateFormat } from "~/utils/index";
import md5 from "js-md5";
dateFormat();
export default {
  name: "articleContent",
  async asyncData({ params, error, redirect }) {
    try {
      const { id } = params;
      // 去掉后面的.html  获得存数字部分
      const newId = id.replace(/[^0-9]/ig,"")
      const {
        data: { data }
      } = await axios({
        method: "post",
        url: "https://www.lovemsss.cn/bg/operateWebArticle/getArticleById",
        data: {
          articleId: newId,
          sign: md5(String(newId))
        }
      });
      return { data };
    } catch (err) {
      if (err.data && err.data.status == 401) {
        return redirect("/login");
      } else {
        console.log(err);
        error({ statusCode: 404 });
      }
    }
  },
  data() {
    return {
      articleId: "",
      logoUrl: require("@/assets/article/logo.png"),
      eyegUrl: require("@/assets/article/eye.png"),
      reviewbgUrl: require("@/assets/article/review.png"),
      comments: [],
      value: ""
    };
  },
  head() {
    return {
      title: this.data.title || "恋爱密室",
      meta: [
        {
          hid: "description",
          name: "description",
          content: `${this.data.title}${this.data.articleDesc}`
        },
        {
          name: "keywords",
          content: "恋爱密室，恋爱密室app，恋爱密室官网，恋爱密室下载"
        }
      ]
    };
  },
  mounted() {
    this.getCntent();
  },
  created() {
    // this.getCntent()
  },
  methods: {
    // 获取数据
    getCntent() {
      const articleId = this.$route.params.id;
      // 去掉后面的.html  获得存数字部分
      this.articleId = articleId.replace(/[^0-9]/ig,"");

      if (this.data.commentNum > 0) {
        // 获取评论
        this.getCetcomments();
      }
    },
    // 获取评论
    getCetcomments() {
      axios
        .post("https://www.lovemsss.cn/bg/operateWebArticle/pageListComment", {
          articleId: this.articleId,
          page: 1,
          pageSize: 9999,
          sign: md5(String(1))
        })
        .then(res => {
          const h = this.$createElement;
          const states = res.data.status;
          switch (states) {
            case 200:
              this.comments = res.data.data.records;
              break;

            default:
              this.value = "";
              this.$message({
                type: "error",
                message: h("p", null, [
                  // h("span", null, "提交失败！！！ "),
                  h("i", { style: "color: red" }, res.data.msg)
                ])
              });
              break;
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    // 发表评论
    review_btn() {
      if (this.value == "") {
        return;
      }
      axios
        .post("https://www.lovemsss.cn/bg/operateWebArticle/addComment", {
          content: this.value,
          parentId: 0,
          articleId: this.articleId
        })
        .then(res => {
          const h = this.$createElement;
          const states = res.data.status;
          switch (states) {
            case 200:
              this.value = "";
              this.getCetcomments();
              this.$message({
                type: "success",
                message: h("p", null, [h("span", null, "提交成功！！！ ")])
              });
              break;

            default:
              this.value = "";
              this.$message({
                type: "error",
                message: h("p", null, [
                  h("span", null, "提交失败！！！ "),
                  h("i", { style: "color: red" }, res.data.msg)
                ])
              });
              break;
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    }
  }
};
</script>
<style scoped>
header {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 999;
  background: rgba(102, 140, 249, 0.8);
  color: white;
}

header a {
  color: white;
}

.container-fluid {
  padding: 0;
  display: flex;
  justify-content: center;
}

.navbar {
  width: 70%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 99999;
  padding: 10px 0;
}
.navigation-row {
  flex-grow: 1;
}
#nav {
  display: flex;
  padding-left: 0;
  margin-bottom: 0;
  list-style: none;
  justify-content: space-around;
}

.navbar-nav li {
  margin-left: 30px;
}

a {
  text-decoration: none;
}

.logo {
  display: flex;
  align-items: center;
}

.logo span {
  color: white;
  margin-left: 0.5rem;
  font-weight: 700;
  font-size: 32px;
}

.slogan {
  width: 1000px;
  padding-bottom: 35px;
}

.slogan > div:first-child {
  color: white;
  font-size: 26px;
  padding-bottom: 15px;
}

.slogan > div:last-child {
  color: #00b9df;
  font-size: 32px;
}

#slider_one {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
}
h1 {
  font-size: 2rem;
}
header {
  position: relative;
}

body {
  position: relative;
  margin: 0;
  padding: 0;
}

.article_title {
  border-bottom: 1px solid #d6d6d6;
  border-top: 1px solid #d6d6d6;
  margin-top: 36px;
  padding: 10px 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}

.article_title > div {
  width: 50%;
}

.title_text {
  font-size: 24px;
  font-weight: 700;
  padding-bottom: 10px;
}

.article_data {
  display: flex;
  justify-content: space-between;
  flex-direction: row;
}

.article_creatTime {
  color: gray;
  font-size: 14px;
}

.article_data_num {
  display: flex;
  align-items: center;
}

.article_data_num > div {
  display: flex;
  align-items: center;
}

.article_data_num > div:first-child {
  padding-right: 20px;
}

.article_data_num > div > span {
  padding-right: 20px;
  font-size: 14px;
  color: gray;
}

.article_data_num > div > img {
  transform: scale(0.8);
}

.article_nav,
.review {
  display: flex;
  width: 100%;
  justify-content: center;
}
.article_nav {
  margin-top: 36px;
}
.article_nav > div {
  width: 50%;
  background: ghostwhite;
}

.review > div {
  width: 50%;
}

input,
button,
select,
textarea {
  outline: none;
}

textarea {
  resize: none;
  width: 99%;
}

.review > div > div:first-child {
  font-size: 20px;
  font-weight: 700;
  padding: 10px 0;
}
.review > div > div:last-child {
  display: flex;
  justify-content: flex-end;
}
.button {
  background-color: #4caf50;
  border: none;
  color: white;
  padding: 8px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 5px;
}
.review_list {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  padding-bottom: 150px;
  width: 100%;
}
.review_list > div {
  width: 50%;
}
.review_list > div {
  padding: 15px 0;
  border-bottom: 1px solid #d6d6d6;
}
.allList_text > div {
  font-size: 20px;
  font-weight: 700;
  padding: 10px 0;
}
.allList_text {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  flex-direction: column;
  align-items: center;
  width: 100%;
}
.allList_text > div {
  width: 50%;
  border-bottom: 1px solid #d6d6d6;
}
.review_content {
  font-size: 14px !important;
  color: gray;
  padding-bottom: 10px !important;
}
.review_time {
  font-size: 10px;
  color: gray;
}

/* 弹窗 */
.showdelog {
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  width: 100%;
}
.showdelog > div {
  background: rgba(122, 191, 238, 0.5);
  padding: 15px 40px;
  font-family: "Droid Sans", sans-serif;
  -webkit-border-radius: 25px;
  -moz-border-radius: 25px;
  border-radius: 25px;
  margin: 40px;
  /* box-shadow: inset 0 1px 0 0 rgba(255,255,255,0.5), 0 0 1px 1px rgba(255,255,255,0.8), 0 0 0 6px rgba(0,0,0,0.06), 0 0 0 3px rgba(0,0,0,0.1); */
  cursor: pointer;
}
.appnav {
  display: none;
}
p {
  text-align: left;
}
@media (max-width: 960px) {
  * {
    margin: 0;
    padding: 0;
  }
  body {
    min-height: auto;
    min-width: auto;
  }
  textarea {
    border: 1px solid gray;
  }
  p img {
    width: 100%;
  }
  .logo span {
    font-size: 16px;
  }
  .logo img {
    transform: scale(0.6);
  }
  .navbar,
  .review > div,
  .allList_text > div,
  .review_list > div,
  .article_nav > div,
  .article_title > div {
    width: 90%;
  }

  #nav {
    display: none;
  }
  .appnav {
    display: flex;
  }
  .appnav > li {
    list-style: none;
  }
}
</style>
<style>
.article_nav p {
  letter-spacing: 4px;
}
</style>